package com.kly.user.serviceImpl;

import com.kly.user.db.dao.UserBackConfigDao;
import com.kly.user.db.dao.UserPromotionRecordDao;
import com.kly.user.db.entity.UserBackConfigEntity;
import com.kly.user.db.entity.UserPromotionRecordEntity;
import com.kly.user.dto.UserBackDto;
import com.kly.user.dto.UserPromotionRecordDto;
import com.kly.user.service.UserPromotionRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userPromotionRecordService")
public class UserPromotionRecordServiceImpl implements UserPromotionRecordService {

    @Resource
    private UserPromotionRecordDao userPromotionRecordDao;

    @Resource
    private UserBackConfigDao userBackConfigDao;


    @Override
    public int queryCountByPromotionType(String userCode, Integer promotionType) {
        return userPromotionRecordDao.queryCountByPromotionType(userCode, promotionType);
    }

    @Override
    public void addUserPromotionRecord(UserPromotionRecordDto userPromotionRecordDto) {
        UserPromotionRecordEntity userPromotionRecordEntity = new UserPromotionRecordEntity();
        userPromotionRecordEntity.setUserCode(userPromotionRecordDto.getUserCode());
        userPromotionRecordEntity.setTriggerType(userPromotionRecordDto.getTriggerType());
        userPromotionRecordEntity.setPromotionType(userPromotionRecordDto.getPromotionType());
        userPromotionRecordEntity.setOperationStatus(userPromotionRecordDto.getOperationStatus());
        userPromotionRecordEntity.setBuyType(userPromotionRecordDto.getBuyType());
        userPromotionRecordEntity.setBeginDate(userPromotionRecordDto.getBeginDate());
        userPromotionRecordEntity.setEndDate(userPromotionRecordDto.getEndDate());
        userPromotionRecordDao.insert(userPromotionRecordEntity);
    }

    @Override
    public UserPromotionRecordDto getLastByUserCode(String userCode) {
        UserPromotionRecordEntity userPromotionRecordEntity = userPromotionRecordDao.getLastByUserCode(userCode);
        return getUserPromotionRecordDto(userPromotionRecordEntity);
    }

    @Override
    public UserBackDto getUserBackByUserCode(String userCode) {
        UserBackConfigEntity userBackConfigEntity = userBackConfigDao.getUserBackByUserCode(userCode);
        if (userBackConfigEntity != null) {
            UserBackDto userBackDto = new UserBackDto();
            userBackDto.setId(userBackConfigEntity.getId());
            userBackDto.setPromotionType(userBackConfigEntity.getPromotionType());
            userBackDto.setShowType(userBackConfigEntity.getShowType());
            userBackDto.setNoticeStatus(userBackConfigEntity.getNoticeStatus());
            return userBackDto;
        }
        return null;
    }

    @Override
    public void  updateUserBack(UserBackDto userBackDto) {
        UserBackConfigEntity userBackConfigEntity = new UserBackConfigEntity();
        userBackConfigEntity.setId(userBackDto.getId());
        userBackConfigEntity.setNoticeStatus(userBackDto.getNoticeStatus());
        userBackConfigDao.updateByPrimaryKey(userBackConfigEntity);
    }


    @Override
    public void UpdateUserPromotionRecord(UserPromotionRecordDto userPromotionRecordDto) {
        UserPromotionRecordEntity userPromotionRecordEntity = new UserPromotionRecordEntity();
        userPromotionRecordEntity.setUserCode(userPromotionRecordDto.getUserCode());
        userPromotionRecordEntity.setTriggerType(userPromotionRecordDto.getTriggerType());
        userPromotionRecordEntity.setPromotionType(userPromotionRecordDto.getPromotionType());
        userPromotionRecordEntity.setBeginDate(userPromotionRecordDto.getBeginDate());
        userPromotionRecordEntity.setEndDate(userPromotionRecordDto.getEndDate());
        userPromotionRecordEntity.setOperationStatus(userPromotionRecordDto.getOperationStatus());
        userPromotionRecordEntity.setId(userPromotionRecordDto.getId());
        userPromotionRecordDao.updateByPrimaryKey(userPromotionRecordEntity);
    }

    @Override
    public UserPromotionRecordDto getUserPromotion(String userCode) {
        UserPromotionRecordEntity userPromotionRecordEntity = userPromotionRecordDao.getUserPromotion(userCode);
        return getUserPromotionRecordDto(userPromotionRecordEntity);
    }

    private UserPromotionRecordDto getUserPromotionRecordDto(UserPromotionRecordEntity userPromotionRecordEntity) {
        if (userPromotionRecordEntity != null) {
            UserPromotionRecordDto userPromotionRecordDto = new UserPromotionRecordDto();
            userPromotionRecordDto.setId(userPromotionRecordEntity.getId());
            userPromotionRecordDto.setUserCode(userPromotionRecordEntity.getUserCode());
            userPromotionRecordDto.setTriggerType(userPromotionRecordEntity.getTriggerType());
            userPromotionRecordDto.setPromotionType(userPromotionRecordEntity.getPromotionType());
            userPromotionRecordDto.setOperationStatus(userPromotionRecordEntity.getOperationStatus());
            userPromotionRecordDto.setBeginDate(userPromotionRecordEntity.getBeginDate());
            userPromotionRecordDto.setEndDate(userPromotionRecordEntity.getEndDate());
            userPromotionRecordDto.setBuyType(userPromotionRecordEntity.getBuyType());
            return userPromotionRecordDto;
        }
        return null;
    }

    @Override
    public int getUserPromotionCount(String userCode) {
        return userPromotionRecordDao.getUserPromotionCount(userCode);
    }
}
